package com.wwh.furns.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author Albert
 * @version 1.0
 * @date 2023/12/5-14:19
 * @describe 基于druid数据库连接池的工具类
 */
public class JDBCUtilsByDruid {
    private static DataSource ds;
    //定义属性ThreadLocal，这里存放一个Connection
    private static ThreadLocal<Connection> threadLocalConn = new ThreadLocal<>();

    //在静态代码块完成ds的初始化
    static {
        Properties properties = new Properties();
        try {
            // 1.目前我们是javaweb方式启动
            // 2.所以要获取src目录下的文件，需要使用类加载器
            properties.load(JDBCUtilsByDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接
    //public static Connection getConnection() throws SQLException {
    //    return ds.getConnection();
    //}

    /**
     * 从threadLocal中获取连接，保证同一个线程使用同一个连接，从而进行事务管理
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() {
        Connection connection = threadLocalConn.get();
        if (connection == null) {
            try {
                connection = ds.getConnection();
                connection.setAutoCommit(false);//设置不自动提交，即手动提交
            } catch (SQLException e) {
                e.printStackTrace();
            }
            threadLocalConn.set(connection);
        }
        return connection;
    }

    //关闭连接。连接池的连接关闭不是真的关闭，而是把连接放回连接池。
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 提交事务
     */
    public static void commit() {
        Connection connection = threadLocalConn.get();
        if (connection != null) {
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();//关闭连接
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            //1.当提交后，需要把connection从threadLocalConn清除掉
            //2.不然，会造成threadLocalConn长时间持有该连接，会影响效率
            //3.也因为我们Tomcat底层使用的是线程池技术
            threadLocalConn.remove();
        }
    }

    /**
     * 回滚操作
     */
    public static void rollback() {
        Connection connection = threadLocalConn.get();
        if(connection != null){
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
